﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineStore.Data_Access_Layer;
using System.Data.Objects;
using System.Windows.Forms;


namespace OnlineStore.Business_Logic_Layer
{
    class BizPurchase
    {
        private OnlineStoreEntities eOnlineStoreEntities;

        public BizPurchase()
        {
            eOnlineStoreEntities = new OnlineStoreEntities();
        }

        public void GetMaxPurchaseID()
        {            
            throw new System.NotImplementedException();
        }
        // Get Order by Order_ID => Get Order by key word
        public List<tbPurchase> GetPurchasebyKeyWord(string strKey, string strValue)
        {
            List<tbPurchase> PurchaseList = new List<tbPurchase>();
         
            try
            {
                if (strKey == "")
                    PurchaseList = eOnlineStoreEntities.tbPurchases.ToList();

                if (strKey == "ID")
                {
                    int nID = int.Parse(strValue);
                    PurchaseList = eOnlineStoreEntities.tbPurchases.Where(i => i.Purchase_ID == nID).ToList();
                }

                if (strKey == "Status")
                    PurchaseList = eOnlineStoreEntities.tbPurchases.Where(i => i.Status == strValue).ToList();

                if (strKey == "Supplier Name")
                    PurchaseList = eOnlineStoreEntities.tbPurchases.Where(i => i.Supplier_Name == strValue).ToList();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            return PurchaseList;
        }
        // Get purchase detail by purchase_id
        public List<tbPurchaseDetail> GetPurchaseDetailbyPurchaseID(int nPurchaseID)
        {
            List<tbPurchaseDetail> PurchaseDetailList = new List<tbPurchaseDetail>();

            PurchaseDetailList = eOnlineStoreEntities.tbPurchaseDetails.Where(i => i.Purchase_ID == nPurchaseID).ToList();
            
            return PurchaseDetailList;
        }
        // update status of purchase after receive stock
        public void UpdatePurchaseStatus(string strNewStatus, int nPurchaseID)
        {
            tbPurchase ePurchase = new tbPurchase();
            ePurchase = GetPurchasebyKeyWord("ID", nPurchaseID.ToString()).First();
            ePurchase.Status = strNewStatus;
            eOnlineStoreEntities.SaveChanges();
        }
        public int ValidateData(string strPurchaseID, DataGridViewRowCollection PurchaseDetail)
        {
            try
            {
                int.Parse(strPurchaseID);                
            }
            catch (Exception ex)
            {
                ex.ToString();
                return -1; // Purchase ID is not number...
            }
            List<tbPurchase> eListPurchase = new List<tbPurchase>();
            eListPurchase = GetPurchasebyKeyWord("ID",strPurchaseID);
            if (eListPurchase.Count > 0)
                return -2; // Purchase ID is existed...            

            return 0; // true
        }
        public int SavePurchase(string strPurchaseID, string strSuppName, string strSuppAdd, string strShipTo, string CreatedDate, DataGridViewRowCollection PurchaseDetail, string strStatus)
        {
            try
            {
                // save purchase
                tbPurchase ePurchase = new tbPurchase();
                OnlineStoreEntities eOnlineStoreEntities = new OnlineStoreEntities();
                ePurchase.Purchase_ID = int.Parse(strPurchaseID);
                ePurchase.Supplier_Name = strSuppName;
                ePurchase.Supplier_Address = strSuppAdd;
                ePurchase.Ship_To = strShipTo;
                ePurchase.Created_Date = DateTime.Parse(CreatedDate);
                ePurchase.Status = strStatus;                
                eOnlineStoreEntities.AddTotbPurchases(ePurchase);
                eOnlineStoreEntities.SaveChanges();
                // save purchase detail
                return SavePurchaseDetail(strPurchaseID, PurchaseDetail);
            }
            catch (Exception ex)
            {
                ex.ToString();
                return -1;
            }            
        }
        private int SavePurchaseDetail(string strPurchaseID, DataGridViewRowCollection PurchaseDetail)
        {
            try
            {
                int nPurchaseID = int.Parse(strPurchaseID);
                BizProduct eBizProduct = new BizProduct();
                
                //OnlineStoreEntities eOnlineStoreEntities = new OnlineStoreEntities();
                for (int i = 0; i < PurchaseDetail.Count; i++)
                {
                    tbPurchaseDetail ePurchaseDetail = new tbPurchaseDetail();
                    ePurchaseDetail.Purchase_ID = nPurchaseID;
                    ePurchaseDetail.Product_ID = int.Parse(PurchaseDetail[i].Cells[0].Value.ToString());
                    ePurchaseDetail.Qty = int.Parse(PurchaseDetail[i].Cells[4].Value.ToString());
                    ePurchaseDetail.Price = int.Parse(PurchaseDetail[i].Cells[5].Value.ToString());
                    ePurchaseDetail.Receive_Qty = 0;
                    eOnlineStoreEntities.AddTotbPurchaseDetails(ePurchaseDetail);
                    eOnlineStoreEntities.SaveChanges();

                    // Update purchase qty on product.
                    eBizProduct.UpdatePurchaseQty(PurchaseDetail[i].Cells[0].Value.ToString(), int.Parse(PurchaseDetail[i].Cells[4].Value.ToString()));
                }                                
            }
            catch (Exception ex)
            {
                ex.ToString();
                return -1;
            }
            return -1;
        }
        
    }
}

